//
//  YHTimeViewController.m
//  ZhuKuang
//
//  Created by jjkj on 17/7/11.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "YHTimeViewController.h"
#import "YHTimeCell.h"
#import "YHTimeSubCell.h"
#import "YHTimeIolModel.h"
#import "YHTimeSubView.h"
#import "SZCalendarPicker.h"
#import "IolDetailModel.h"
@interface YHTimeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    /**列表数组*/
    NSArray * _list;
    /**日历选择框*/
    SZCalendarPicker * _calendarPicker;
}
/**列表*/
@property (weak, nonatomic) IBOutlet UITableView *timeTableView;
/**时间*/
@property (nonatomic , strong) NSString * timeString;
/**空数据label*/
@property (weak, nonatomic) IBOutlet UILabel *nullLabel;
/** 详情、报警详情*/
@property (nonatomic , strong) YHTimeSubView *subDetailView;
@end

@implementation YHTimeViewController

#pragma mark - interface
-(void)getData
{
    [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
   [ClientRequest SOAPMthod:getIolCurre_Copy
                   soapBody:@{@"time":self.timeString,
                              @"power":[UserLoginModel getPower]}
                    success:^(id responseObject) {
                        if ([DataCheck isValidDictionary:responseObject]) {
                            _list = [YHTimeIolModel mj_objectArrayWithKeyValuesArray:responseObject[@"iolList"]];
                            [self.timeTableView reloadData];
                            if ([DataCheck isValidArray:_list]) {
                                self.nullLabel.hidden = YES;
                            }else{
                                self.nullLabel.hidden = NO;
                            }
                        }
                        [MBProgressHUD hideLoadViewWithView:kWindow];
                    } failure:^(NSError *error) {
                        [MBProgressHUD hideLoadViewWithView:kWindow];
                    }];
}

#pragma mark - 懒加载
-(YHTimeSubView *)subDetailView
{
    if (!_subDetailView) {
        _subDetailView = [YHTimeSubView viewFromXib];
    }
    return _subDetailView;
}

#pragma mark - 设置标题、时间选择

/**
 设置标题

 @param timeString 标题文案
 */
-(void)setNavigationTitle:(NSString *)timeString
{
    if ([DataCheck isValidString:timeString]) {
        self.timeString = timeString;
    }else{
        self.timeString = [CommClass formatTimeStamp:[NSDate date] timeFormat:NEW_TIME_FORMATTER_DATE];
        UIButton * navBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        navBtn.width = 120;
        navBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [navBtn addTarget:self action:@selector(dateSelectdAction:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.titleView = navBtn;
    }
    UIButton * navItemBtn = (UIButton *)self.navigationItem.titleView;
    [navItemBtn setTitle:self.timeString forState:UIControlStateNormal];
    [self getData];
}


/**
 导航栏点击弹出时间选择框事件

 @param sender 点击对象
 */
-(void)dateSelectdAction:(UIButton *)sender
{
    _calendarPicker= [SZCalendarPicker showOnView:kWindow];
    _calendarPicker.today = [NSDate date];
    _calendarPicker.date = [CommClass dateFormatTimeStamp:self.timeString timeFormat:NEW_TIME_FORMATTER_DATE];
    _calendarPicker.frame = CGRectMake(0, 0, kScreenWidth, 370);
    __weak typeof(self) weakSlef = self;
    //选择日期的回调函数
    _calendarPicker.calendarDateBlock = ^(NSDate * date){
        [weakSlef setNavigationTitle:[CommClass formatTimeStamp:date timeFormat:NEW_TIME_FORMATTER_DATE]];
    };
}


#pragma mark - view
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 20, 20);
    [backBtn setImage:[UIImage imageNamed:@"leftArrow"] forState:UIControlStateNormal];
    [backBtn addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.timeTableView registerNib:[UINib nibWithNibName:YHTimeCellIdentifier bundle:nil] forCellReuseIdentifier:NSStringFromClass([YHTimeCell class])];
    [self.timeTableView registerNib:[UINib nibWithNibName:YHTimeSubCellIdentifier bundle:nil] forCellReuseIdentifier:NSStringFromClass([YHTimeSubCell class])];
    [self setNavigationTitle:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _list.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([DataCheck isValidArray:_list]) {
        YHTimeIolModel * model = _list[section];
        return model.singleList.count+globalNum1;
    }
    return globalNum0;
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == globalNum0) {
        return YHTimeCellHeight;
    }
    return YHTimeSingleCellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (indexPath.row == globalNum0) {
        YHTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YHTimeCell class]) forIndexPath:indexPath];
        cell.iolModel = _list[indexPath.section];
        cell.detailClickBlock = ^(){
            [weakSelf showDetailView:_list[indexPath.section]];
        };
        cell.singleClickBlock = ^(){
            [weakSelf showSingleView:_list[indexPath.section] withSubModel:nil];
        };
        return cell;
    }else{
        YHTimeSubCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YHTimeSubCell class])];
        YHTimeIolModel * model = _list[indexPath.section];
        cell.section = indexPath.section;
        cell.singleModel = model.singleList[indexPath.row-1];
        cell.isLast = indexPath.row==model.singleList.count?YES:NO;
        cell.singleClickBlock = ^(){
            [weakSelf showSingleView:model withSubModel:model.singleList[indexPath.row-1]];
        };
        return cell;
    }
}

#pragma mark - 详情点击事件
/**
 * 弹出详情
 * model 数据
 */

-(void)showDetailView:(YHTimeIolModel *)model
{
    if ([model.type containsString:@"入油"]) {
        [self.subDetailView setType:NO withData:[IolDetailModel getEnterIolDetail:model] withTitle:model.type withIolType:NO];
    }else if ([model.type containsString:@"出油"]){
        [self.subDetailView setType:NO withData:[IolDetailModel getOutIolDetail:model] withTitle:model.type withIolType:NO];
    }else{
        [self.subDetailView setType:NO withData:[IolDetailModel getRefuelIolDetail:model] withTitle:model.type withIolType:NO];
    }
    
}

/**
 * 弹出报警详情
 * model 数据
 */

-(void)showSingleView:(YHTimeIolModel *)model withSubModel:(YHTimeSingleModel *)subModel
{
    NSString * title = [model.type stringByAppendingString:@"异常"];
    if (subModel) {
        title = [title stringByReplacingCharactersInRange:NSMakeRange(0, 0) withString:subModel.iolnum];
        [self.subDetailView setType:YES withData:[IolDetailModel getSingleIolDetail:subModel withType:model.type] withTitle:title withIolType:YES];
    }else{
        [self.subDetailView setType:YES withData:[IolDetailModel getCarRefuelIolAlarmDetail:model] withTitle:title withIolType:NO];
    }
    
}


@end
